package com.example.smartclassroom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartclassroom.entity.Course;
import com.example.smartclassroom.enums.BorrowStatusEnum;
import com.example.smartclassroom.mapper.CourseMapper;
import com.example.smartclassroom.mapper.RoomMapper;
import com.example.smartclassroom.service.CourseService;
import com.example.smartclassroom.util.JsonDate;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Primary
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private RoomMapper roomMapper;
    /**
     * 管理员查询所有课程
     * @param course
     * @return
     */
    public JsonDate queryAllCourse(Course course) {
        JsonDate date=new JsonDate();
        date.setAge1(courseMapper.selectTotal(course));
        date.setData(courseMapper.selectAll(course));
        return date;
    }

    /**
     * 管理员新增课程
     * 判断教室时间是否冲突
     * 1、教室大小是否合适
     * 2、开始结束时间比较
     * 3、星期比较
     * 4、具体时间比较
     * @param course
     * @return
     */
    public JsonDate addCourse(Course course) {
        JsonDate date=new JsonDate();

//        1、教室大小是否合适
        if (course.getNum()>roomMapper.queryRoomById(course.getRoomId()).getRoomNum()){
            //教室不合适
            return JsonDate.errorEnum(BorrowStatusEnum.ROOM_NUM_ERROR);
        }else {
            List<Course> CourseList=courseMapper.selectCourseByRoomId(course.getRoomId());
            //循环遍历
            int judgment= 0 ;
            for (Course course1:CourseList){
                //        2、开始结束时间比较
                //2.1新开始在旧开始后、新结束在旧结束前
                //2.2新开始在旧开始前、新结束在旧结束后   ||(course.getStartTime().isBefore(course1.getStartTime())&&course.getEndTime().isAfter(course1.getStartTime()))
                //2.3新开始在旧结束前、新结束在就结束后
                //2.4新开始等于旧开始、新结束等于旧结束
                //2.5新开始在旧结束前、新结束等于旧结束
                //2.6新开始等于就开始、新结束在旧开始后
                if ((course.getStartTime().isAfter(course1.getStartTime())&&course.getEndTime().isBefore(course1.getEndTime()))
                        ||(course.getStartTime().isBefore(course1.getStartTime())&&course.getEndTime().isAfter(course1.getStartTime()))
                        ||(course.getStartTime().isBefore(course1.getEndTime())&&course.getEndTime().isAfter(course1.getEndTime()))
                        ||(course.getStartTime().equals(course1.getStartTime())&&course.getEndTime().equals(course1.getEndTime()))
                        ||(course.getStartTime().isBefore(course1.getEndTime())&&course.getEndTime().equals(course1.getEndTime()))
                        ||(course.getStartTime().equals(course1.getStartTime())&&course.getEndTime().isAfter(course1.getStartTime()))

                ){
                    //        3、星期比较
                    if (course.getWeekDay().equals(course1.getWeekDay())){
                        //        4、具体时间比较
                        if (course.getSegment().equals(course1.getSegment())){
                            judgment = 0;
                            return JsonDate.errorEnum(BorrowStatusEnum.ROOM_SEGMENT_ERROR);
                        }else {
                            judgment = 1;
                        }
                    }else {
                        judgment = 1;
                    }
                }else{
                    judgment = 1;
                }
            }
            //如果judgment==1添加课程
            if (judgment==1){
                date.setData(courseMapper.insert(course));
            }
        }
        return date;
    }

    /**
     * 删除课程
     * @param ids
     * @return
     */
    public JsonDate deleteCourse(Integer[] ids){
        JsonDate date=new JsonDate();
        Integer rows=courseMapper.deleteBatchIds(Arrays.asList(ids));
        if (rows>0){
            date.setCode(0);
            date.setMsg("success");
        }else {
            date.setCode(1);
            date.setMsg("fail");
        }
        return date;
    }

    /**
     * 根据id修改课程
     * @param course
     * @return
     */
    public JsonDate updateCourseById(Course course) {
        JsonDate date=new JsonDate();
        Integer rows=courseMapper.updateById(course);
        if (rows>0){
            date.setCode(0);
            date.setMsg("success");
        }else {
            date.setCode(1);
            date.setMsg("fail");
        }
        return date;
    }


}
